<template>
  <div>
    <h5 style="text-align: left">数据筛选</h5>
    <div>
      <!-- 查询条件 -->
      <table>
        <tr style="height: 80px">
          <td>报损编号:</td>
          <td>
            <el-input
              v-model="frmLossCode"
              placeholder="请输入产品编号"
            ></el-input>
          </td>
          <td style="width: 80px">报损类型:</td>
          <td>
            <el-input
              v-model="frmLossType"
              placeholder="请输入产品名称"
            ></el-input>
          </td>
          <td style="width: 80px">状态:</td>
          <td>
            <el-radio-group v-model="state">
              <el-radio :label="1">已审核</el-radio>
              <el-radio :label="2">未审核</el-radio>
            </el-radio-group>
          </td>
          <td style="width: 120px">
            <el-button type="primary" @click="onFromlossList()">查询</el-button>
          </td>
          <td style="width: 150px">
            <el-button type="success" plain @click="onAddFrmloss()">
              <i class="el-icon-plus"></i>
              添加
            </el-button>
          </td>
          <td style="width: 150px">
            <el-button type="danger" plain @click="onBathdel()">
              <i class="el-icon-delete-solid"></i>
              批量删除
            </el-button>
          </td>
          <td style="width: 150px">
            <el-button plain>
              <i class="el-icon-upload"> </i>
              导出
            </el-button>
          </td>
        </tr>
      </table>
    </div>

    <!-- 报损列表 -->
    <div style="width: 100%; margin-top: 20px">
      <!-- 报损列表 -->
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="frmLossCode" label="报损单号" align="center">
        </el-table-column>
        <el-table-column prop="productName" label="产品名称" align="center">
        </el-table-column>
        <el-table-column prop="productCode" label="产品编号" align="center">
        </el-table-column>
        <el-table-column prop="batch" label="批次" align="center">
        </el-table-column>
        <el-table-column prop="unit" label="单位" align="center">
        </el-table-column>
        <el-table-column prop="creator" label="制单人" align="center">
        </el-table-column>
        <el-table-column prop="creatorTime" label="制单时间" align="center">
        </el-table-column>
        <el-table-column prop="inWarehouseNumber" label="数量" align="center">
        </el-table-column>
        <el-table-column prop="name" label="仓库" align="center">
        </el-table-column>
        <el-table-column prop="locationName" label="库位" align="center">
        </el-table-column>
        <el-table-column prop="frmLossType" label="报损类型" align="center">
        </el-table-column>
        <el-table-column prop="state" label="状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.state == 1">待审核</span>
            <span v-if="scope.row.state == 2">审核成功</span>
            <span v-if="scope.row.state == 3">审核失败</span>
          </template>
        </el-table-column>
        <el-table-column prop="reviewer" label="审核人" align="center">
        </el-table-column>
        <el-table-column prop="audittime" label="审核时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.audittime.substring(0, 10) }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="280px">
          <template slot-scope="scope">
            <el-link
              type="primary"
              class="el-icon-view"
              @click="onDetails(scope.row.frmLossCode)"
              style="width: 60px"
              >查看</el-link
            >
            <el-link type="success" class="el-icon-edit" style="width: 60px"
              >编辑</el-link
            >
            <el-link
              type="warning"
              class="el-icon-delete"
              style="width: 60px"
              @click="onDelete(scope.row.id)"
              >删除</el-link
            >
            <el-link type="danger" class="el-icon-user">审核</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        prev-text="上一页"
        next-text="下一页"
      >
      </el-pagination>
    </div>

    <!-- 报损弹出框 -->
    <el-dialog title="报损详细信息" :visible.sync="dialogTableVisible">
      <div>
        <el-descriptions data:FrmlossList>
          <el-descriptions-item label="报损单号">
            {{ this.FrmlossList.frmLossCode }}
          </el-descriptions-item>
          <el-descriptions-item label="制单人">
            {{ this.FrmlossList.creator }}
          </el-descriptions-item>
          <el-descriptions-item label="报损类型">
            {{ this.FrmlossList.frmLossType }}
          </el-descriptions-item>
          <el-descriptions-item label="制单日期">
            {{ this.FrmlossList.creatorTime }}
          </el-descriptions-item>
          <el-descriptions-item label="备注">
            {{ this.FrmlossList.remarks }}
          </el-descriptions-item>
          <el-descriptions-item label="审核原因">
            {{ this.FrmlossList.opinion }}
          </el-descriptions-item>
        </el-descriptions>
        <!-- 产品列表 -->
        <el-table :data="FrmlossList.listProduct" border style="width: 100%">
          <el-table-column prop="productCode" label="产品编号" align="center">
          </el-table-column>
          <el-table-column prop="productName" label="产品名称" align="center">
          </el-table-column>
          <el-table-column prop="unit" label="单位" align="center">
          </el-table-column>
          <el-table-column prop="frmLossNum" label="报损数量" align="center">
          </el-table-column>
          <el-table-column prop="locationName" label="当前库位" align="center">
          </el-table-column>
        </el-table>
        <el-button type="primary" @click="dialogTableVisible = false">
          确认
        </el-button>
        <el-button @click="dialogTableVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  GetFrmlossapi,
  DeleteFrmlapi,
  BatchDeleteFrmlapi,
  FrmlDetailapi,
} from "@/api/WarehouseServer/LossReporting";
export default {
  data() {
    return {
      tableData: [],
      pageIndex: 1,
      pageSize: 5,
      totalCount: 0,
      pageCount: 0,
      frmLossCode: "",
      frmLossType: "",
      state: 0,
      multipleSelection: [], //复选框

      //报损列表
      FrmlossList: {
        listProduct: [],
      },
      dialogTableVisible: false,
    };
  },
  methods: {
    //加载报损列表
    onFromlossList() {
      var dto = {
        frmLossCode: this.frmLossCode,
        frmLossType: this.frmLossType || null,
        state: this.state,
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
      };
      GetFrmlossapi(dto).then((res) => {
        console.log(1111111111,res);
        this.tableData = res.data.data.result;
        this.totalCount = res.data.totalCount;
        this.pageCount = res.data.pageCount;
      });
    },

    //多选框
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    //分页操作
    handleSizeChange(val) {
      this.pageSize = val;
      this.onFromlossList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.onFromlossList();
    },

    //删除报损信息
    onDelete(id) {
      this.$confirm("此操作将永久删除报损信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var dto = {
            id: id,
          };
          DeleteFrmlapi(dto).then((res) => {
            if (res.data.totalCount > 0) {
              this.$message.success("删除成功");
              this.onFromlossList();
            } else {
              this.$message("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //批量删除
    onBathdel() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("必须选择一项删除");
        return;
      }
      this.$confirm("确定要批量删除产品信息吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var ids = [];
          this.multipleSelection.forEach((element) => {
            ids.push(element.id);
          });
          var idobj = {
            id: ids.toString(),
          };
          BatchDeleteFrmlapi(idobj).then((res) => {
            if (res.data.totalCount > 0) {
              this.$message.success("批量删除成功");
              this.onFromlossList();
            } else {
              this.$message("批量删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //查看报损详情
    onDetails(frmLossCode) {
      this.dialogTableVisible = true;
      var id = {
        FrmLossCode: frmLossCode,
      };
      FrmlDetailapi(id).then((res) => {
        this.FrmlossList = res.data.data;
      });
    },

    //跳转添加
    onAddFrmloss() {
      this.$router.push("FrmlossAdd");
    },
  },
  created() {
    //加载报损列表
    this.onFromlossList();
  },
};
</script>

<style >
.del {
  color: red;
}
.edit {
  color: orange;
}
</style>